/**
 * 
 */
package com.dovgopoly.carlogbook.services.security;

import java.util.ArrayList;
import java.util.HashMap;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.dovgopoly.carlogbook.dto.UserDetailsDTO;

/**
 * @author Zhenya
 * 
 */
@Service("userDetailsServiceImpl")
@Transactional
public class UserDetailsServiceImpl implements UserDetailsService {

	private static final HashMap<String, String> users;;
	static {
		users = new HashMap<String, String>();
		users.put("user1", "pwd1");
		users.put("user2", "pwd2");
		users.put("user3", "pwd3");
	}

	public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {

		if (users.get(username) == null) {
			return null;
		}

		ArrayList<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>();

		GrantedAuthority authority = new SimpleGrantedAuthority("ROLE_USER");
		authorities.add(authority);
		UserDetailsDTO userDetailsDTO = new UserDetailsDTO(username, users.get(username), authorities);

		return userDetailsDTO;
	}

}
